# try-except模块可以加else，如下面的例子，当没有任何错误发生时，也会执行 else 里的语句
try:
    print('try...')
    r = 10 / int('2')
    print('result:', r)
except ValueError as e:
    print('ValueError:', e)
except ZeroDivisionError as e:
    print('ZeroDivisionError:', e)
else:
    print('no error!')
finally:
    print('finally...')
print('END')

# 记录错误栈信息可以用logging模块
import logging
def foo(s):
    return 10 / int(s)

def bar(s):
    return foo(s) * 2
def main():
    bar('0')

try:
    main()
except Exception as e:
    logging.exception(e)

print('END')

from functools import reduce

def str2num(s):
    return int(s)

def calc(exp):
    ss = exp.split('+')
    ns = map(str2num, ss)
    return reduce(lambda acc, x: acc + x, ns)

def main():
    r = calc('100 + 200 + 345')
    print('100 + 200 + 345 =', r)
    r = calc('99 + 88 + 7.6')
    print('99 + 88 + 7.6 =', r)

try:
    main()
except Exception as e:
    logging.exception(e)


# 调式
import logging
logging.basicConfig(level=logging.INFO)

s = '0'
n = int(s)
logging.info('n = %d' % n)
try:
    print(10 / n)
except Exception as e:
    logging.exception(s)
